package com.kc.sjwh.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Res;
import com.kc.sjwh.entity.po.Role;
import com.kc.sjwh.entity.po.User;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Dict;
import com.kc.sjwh.other.Tool;
import com.kc.sjwh.service.InterfaceService;

/**
 * 对外接口action
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年8月14日
 */
@Controller
@RequestMapping("interface")
public class InterfaceAction {
	@Autowired
	InterfaceService service;
	
	/**
	 * 获取用户在某应用下的所有菜单
	 * @param appCode 所属应用的代码
	 * @param userId 用户id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getMenuList")
	public String getMenuList(String appCode, String userId) {
		List<Res> menuList = service.getMenuList(appCode, userId);
		return JSON.toJSONString(new AjaxRes(menuList));
	}
	@ResponseBody
	@RequestMapping("getMenuListAsHtml")
	public String getMenuListAsHtml(String appCode, String userId) {
		List<Res> menuList = service.getMenuList(appCode, userId);
		return JSON.toJSONString(new AjaxRes(Tool.menusToHtml(menuList)));
	}
	
	/**
	 * 根据账号获取用户
	 * @param acc
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getUserByAcc")
	public String getUserByAcc(HttpServletRequest req, String acc) {
		User user = service.getUserByAcc(acc);
		return JSON.toJSONString(new AjaxRes(JSON.toJSONString(user)));
	}

	/**
	 * 取单位字典
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getOrgans")
	public String getOrgans(HttpServletRequest req) {
		return JSON.toJSONString(new AjaxRes(JSON.toJSONString(Dict.getOrgan().getList())));
	}

	/**
	 * 获取[可选单位]字典
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getOrgans4Choose")
	public String getOrgans4Choose(HttpServletRequest req) {
		return JSON.toJSONString(new AjaxRes(JSON.toJSONString(Dict.getOrgan4Choose().getList())));
	}

	/**
	 * 取警情性质字典
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getJqxzs")
	public String getJqxzs(HttpServletRequest req) {
		return JSON.toJSONString(new AjaxRes(JSON.toJSONString(Dict.getJqxz().getList())));
	}
	

	/**
	 * 根据多个用户id，返回用户对象列表
	 * @param req
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getUsersByIds")
	public String getUsersByIds(HttpServletRequest req, String ids) {
		List<User> users = service.getUsersByIds(ids);
		return JSON.toJSONString(new AjaxRes(JSON.toJSONString(users)));
	}
	
	/**
	 * 根据用户id获取角色s
	 * @param req
	 * @param userId
	 */
	@ResponseBody
	@RequestMapping("getRolesByUserId")
	public String getRolesByUserId(HttpServletRequest req, String appCode, String userId) {
		List<Role> users = service.getRolesByUserId(userId, appCode);
		return JSON.toJSONString(new AjaxRes(JSON.toJSONString(users)));
	}
	
}
